import Vue from 'vue'
import Vuex from 'vuex'

const replaceArray = (oldArr, newArr) => {
	let resultArr = JSON.parse(JSON.stringify(oldArr));
	resultArr.forEach(v => {
		let newItem = newArr.find(d => d.id == v.id);
		for (let key in newItem) {
			if (Array.isArray(newItem[key]) && !Array.isArray(v[key])) {
				v[key] = newItem[key][0];
			} else {
				v[key] = newItem[key];
			}
		}
	});
	return resultArr;
}
const toObj = (data, key) => {
	let obj = {};
	data.forEach(v => {
		obj[v[key]] = v;
	});
	return obj;
};
const stock_toObj = (data) => {
	let obj = {};
	data.forEach(v => {
		obj[v.material_id[0]] = v;
	});
	return obj;
};
const set_material_item_storage = (data) =>{
	let material_item = JSON.parse(JSON.stringify(data));
	let _length = material_item.length;
	let material_item1 = [];
	if(_length>100){
		let half_length = Math.ceil(_length/2);
		material_item1 = material_item.slice(half_length,_length); 
		material_item = material_item.slice(0,half_length);
		// console.log(material_item1);
		// console.log(material_item);
	}
	try{
		uni.setStorageSync('material_item', material_item);
		uni.setStorageSync('material_item1', material_item1);
		// uni.setStorageSync('material_obj', toObj(material_item,'id'));
		// uni.setStorageSync('material_obj1', toObj(material_item1,'id'));
	}catch(e){
		console.log('setStorageSync-material_item:error');
	}
};

const set_dish_key_storage = (data) =>{
	let dish_key = JSON.parse(JSON.stringify(data));
	let _length = dish_key.length;
	let dish_key1 = [];
	if(_length>100){
		let half_length = Math.ceil(_length/2);
		dish_key1 = dish_key.slice(half_length,_length); 
		dish_key = dish_key.slice(0,half_length);
	}
	try{
		uni.setStorageSync('dish_key', dish_key);
		uni.setStorageSync('dish_key1', dish_key1);
		// uni.setStorageSync('dish_key_obj', toObj(dish_key,'id'));
		// uni.setStorageSync('dish_key_obj1', toObj(dish_key1,'id'));
	}catch(e){
		console.log('setStorageSync-dish_key:error');
	}
};
const set_cus_sales_cfg_storage = (data) => {
	let cus_sales_cfg = JSON.parse(JSON.stringify(data));
	let _length = cus_sales_cfg.length;
	let cus_sales_cfg1 = [];
	if(_length>100){
		let half_length = Math.ceil(_length/2);
		cus_sales_cfg1 = cus_sales_cfg.slice(half_length,_length); 
		cus_sales_cfg = cus_sales_cfg.slice(0,half_length);
	}
	try{
		uni.setStorageSync('cus_sales_cfg', cus_sales_cfg);
		uni.setStorageSync('cus_sales_cfg1', cus_sales_cfg1);
	}catch(e){
		console.log('setStorageSync-cus_sales_cfg:error');
	}
};

Vue.use(Vuex) // vue的插件机制

let _userInfo = uni.getStorageSync('userInfo') || null;
let _location = uni.getStorageSync('location') || null;
let _token = uni.getStorageSync('token') || '';
let _role = uni.getStorageSync('role') || [];
let _material_static_data = uni.getStorageSync('material_static_data') || {};
let _material_item = uni.getStorageSync('material_item') || [];
_material_item = _material_item.concat(uni.getStorageSync('material_item1') || []);
let _material_obj = toObj(_material_item,'id') || {};

let _stock = uni.getStorageSync('stock') || [];
let _stock_obj = stock_toObj(_stock) || {};
let _plan_week_model_overall_show = uni.getStorageSync('plan_week_model_overall_show') || {};
let _dish_key = uni.getStorageSync('dish_key') || [];
_dish_key = _dish_key.concat(uni.getStorageSync('dish_key1') || []);
let _dish_key_obj = toObj(_dish_key,'id') || {};

let _dinner_type_category = uni.getStorageSync('dinner_type_category') || [];
let _dinner_type_category_obj = toObj(_dinner_type_category,'id') || {};
let _dish_top_category = uni.getStorageSync('dish_top_category') || [];
let _dish_top_category_obj = toObj(_dish_top_category,'id') || {};
let _number_category = uni.getStorageSync('number_category') || [];
let _number_category_obj = toObj(_number_category,'id') || {};
let _daily_cost_category = uni.getStorageSync('daily_cost_category') || [];
let _daily_cost_category_obj = toObj(_daily_cost_category,'id') || {};
let _material_top_category = uni.getStorageSync('material_top_category') || [];
let _material_second_category = uni.getStorageSync('material_second_category') || [];
let _material_purchase_unit_category = uni.getStorageSync('material_purchase_unit_category') || [];
let _material_purchase_unit_category_obj = toObj(_material_purchase_unit_category,'id') || {};
let _material_item_bom_unit_ratio = uni.getStorageSync('material_item_bom_unit_ratio') || [];
let _msupplier = uni.getStorageSync('msupplier') || [];
let _cus_loc = uni.getStorageSync('cus_loc') || [];
let _sales_category_list = uni.getStorageSync('sales_category_list') || [];
let _cus_sales_cfg  = uni.getStorageSync('cus_sales_cfg') || [];
_cus_sales_cfg = _cus_sales_cfg.concat(uni.getStorageSync('cus_sales_cfg1') || []);
let _sales_cfg  = uni.getStorageSync('sales_cfg') || {};

// Vuex.Store 构造器选项
const store = new Vuex.Store({
	// 为了不和页面或组件的data中的造成混淆，state中的变量前面建议加上$符号
	state: {
		$userInfo: _userInfo, // 用户信息
		$token: _token,
		$location: _location,
		$role: _role, //[]
		$material_static_data: _material_static_data,
		$material_item: _material_item,
		$material_obj: _material_obj,
		$stock: _stock,
		$stock_obj: _stock_obj,
		$plan_week_model_overall_show: _plan_week_model_overall_show,
		$days: [{
				title: '一',
				value: 'day1'
			},
			{
				title: '二',
				value: 'day2'
			},
			{
				title: '三',
				value: 'day3'
			},
			{
				title: '四',
				value: 'day4'
			},
			{
				title: '五',
				value: 'day5'
			},
			{
				title: '六',
				value: 'day6'
			},
			{
				title: '日',
				value: 'day7'
			},
		],
		$dish_key: _dish_key,
		$dish_key_obj: _dish_key_obj,
		$dinner_type_category: _dinner_type_category,
		$dinner_type_category_obj: _dinner_type_category_obj,
		$dish_top_category: _dish_top_category,
		$dish_top_category_obj: _dish_top_category_obj,
		$number_category: _number_category,
		$number_category_obj: _number_category_obj,
		$daily_cost_category: _daily_cost_category,
		$daily_cost_category_obj: _daily_cost_category_obj,
		$material_top_category: _material_top_category,
		$material_second_category:_material_second_category,
		$material_purchase_unit_category: _material_purchase_unit_category,
		$material_purchase_unit_category_obj:_material_purchase_unit_category_obj,
		$material_item_bom_unit_ratio: _material_item_bom_unit_ratio,
		$msupplier:_msupplier,
		$cus_loc:_cus_loc,
		$cus_sales_cfg:_cus_sales_cfg,
		$sales_category_list:_sales_category_list,
		$sales_cfg:_sales_cfg
	},
	mutations: {
		set_sales_cfg(state, data){
			state.$sales_cfg = data;
			try{
				uni.setStorageSync('sales_cfg', data)
			}catch(e){
				console.log('setStorageSync-sales_cfg:error');
			}
		},
		set_role(state, data) {
			state.$role = data;
			try{
				uni.setStorageSync('role', data)
			}catch(e){
				console.log('setStorageSync-role:error');
			}
		},
		set_userInfo(state, data) {
			state.$userInfo = data;
			try{
				uni.setStorageSync('userInfo', data)
			}catch(e){
				console.log('setStorageSync-userInfo:error');
			}
		},
		set_token(state, data) {
			state.$token = data;
			try{
				uni.setStorageSync('token', data)
			}catch(e){
				console.log('setStorageSync-token:error');
			}
		},
		set_location(state, data) {
			state.$location = data;
			uni.setStorageSync('location', data)
		},
		set_material_static_data(state, data) {
			state.$material_static_data = data;
			try{
				uni.setStorageSync('material_static_data', data)
			}catch(e){
				console.log('setStorageSync-material_static_data:error');
			}
		},
		set_cus_loc(state, data){
			state.$cus_loc = data;
			try{
				uni.setStorageSync('cus_loc', data)
			}catch(e){
				console.log('setStorageSync-cus_loc:error');
			}
		},
		set_sales_category_list(state, data){
			state.$sales_category_list = data;
			try{
				uni.setStorageSync('sales_category_list', data)
			}catch(e){
				console.log('setStorageSync-sales_category_list:error');
			}
		},
		set_cus_sales_cfg(state, data){
			state.$cus_sales_cfg = data;
			set_cus_sales_cfg_storage(data);
		},
		set_material_item(state, data) {
			let material_item = data;
			let material_obj = {};
			material_item.forEach(v => {
				if(!v.mname){
					v.mname = v.name.split('-')[0];
				}
				material_obj[v.id] = v;
			})
			state.$material_item = material_item;
			state.$material_obj = material_obj;
			set_material_item_storage(material_item);	
		},
		update_material_item(state, data) {
			let {
				updateArr,
				delIds
			} = data;
			let updateIds = updateArr.map(v => v.id);
			let material_item = state.$material_item;
			let old_material_item = material_item.filter(v => updateIds.includes(v.id));
			// let new_material_item = replaceArray(old_material_item, updateArr);
			let new_material_item = [];
			updateArr.forEach(v => {
				let _Item = old_material_item.find(d => d.id == v.id);
				if(!_Item){
					// 新增
					_Item = {};
				}
				for (let key in v) {
					if(key == 'bom_unit_ratio_ids' || key == 'material_alias_name_list'){
						_Item[key] = v[key];
					}else if(Array.isArray(v[key])){
						_Item[key] = v[key][0];
					}else{
						_Item[key] = v[key];
					}
				}
				if(!_Item.mname){
					_Item.mname = _Item.name.split('-')[0];
				}
				new_material_item.push(_Item);
			});
			// console.log(new_material_item);
			material_item = material_item.filter(v => !updateIds.includes(v.id));
			material_item = material_item.filter(v => !delIds.includes(v.id));
			material_item = material_item.concat(new_material_item);
			state.$material_item = material_item;
			state.$material_obj = toObj(material_item,'id');
			set_material_item_storage(material_item);
			
			// uni.setStorageSync('material_item', material_item);
			// let material_obj = state.$material_obj;
			// new_material_item.forEach(v=>{
			// 	material_obj[v.id] = v;
			// });
			// delIds.forEach(id=>{
			// 	delete material_obj[id];
			// });
			// state.$material_obj = material_obj;
			// uni.setStorageSync('material_obj', material_obj);
		},
		set_stock(state, data) {
			let stock = data;
			state.$stock = stock;
			state.$stock_obj = stock_toObj(stock);
			try{
				uni.setStorageSync('stock', stock)
				// uni.setStorageSync('stock_obj', stock_obj)
			}catch(e){
				console.log('setStorageSync-stock:error');
			}
		},
		update_stock(state, data){
			let {
				updateArr,
				delIds
			} = data;
			let updateIds = updateArr.map(v => v.id);
			let stock = state.$stock;
			let old_stock = stock.filter(v => updateIds.includes(v.id));
			let new_stock = [];
			updateArr.forEach(v => {
				let _Item = old_stock.find(d => d.id == v.id);
				if(!_Item){
					// 新增
					_Item = {};
				}
				for (let key in v) {
					_Item[key] = v[key];
				}
				new_stock.push(_Item);
			});
			stock = stock.filter(v => !updateIds.includes(v.id));
			stock = stock.filter(v => !delIds.includes(v.id));
			stock = stock.concat(new_stock);
			state.$stock = stock;
			state.$stock_obj = stock_toObj(stock);
			try{
				uni.setStorageSync('stock', stock);
				// uni.setStorageSync('stock_obj', stock_obj);
			}catch(e){
				console.log('setStorageSync-stock:error');
			}
		},
		set_plan_week_model_overall_show(state, data) {
			state.$plan_week_model_overall_show = data;
			try{
				uni.setStorageSync('plan_week_model_overall_show', data)
			}catch(e){
				console.log('setStorageSync-plan_week_model_overall_show:error');
			}
		},
		set_dinner_type_category(state, data) {
			let dinner_type_category = data;
			let dinner_type_category_obj = toObj(dinner_type_category,'id');
			state.$dinner_type_category = dinner_type_category;
			state.$dinner_type_category_obj = dinner_type_category_obj;
			try{
				uni.setStorageSync('dinner_type_category', dinner_type_category);
				// uni.setStorageSync('dinner_type_category_obj', dinner_type_category_obj);
			}catch(e){
				console.log('setStorageSync-dinner_type_category:error');
			}
		},
		set_dish_key(state, data) {
			let dish_key = data;
			let dish_key_obj = {};
			dish_key.forEach(v => {
				dish_key_obj[v.id] = v;
			})
			state.$dish_key = dish_key;
			state.$dish_key_obj = dish_key_obj;
			
			set_dish_key_storage(dish_key);
			
			// uni.setStorageSync('dish_key', dish_key)
			// uni.setStorageSync('dish_key_obj', dish_key_obj)
		},
		set_dish_top_category(state, data) {
			let dish_top_category = data;
			let dish_top_category_obj = toObj(dish_top_category,'id');
			state.$dish_top_category = dish_top_category;
			state.$dish_top_category_obj = dish_top_category_obj;
			try{
				uni.setStorageSync('dish_top_category', dish_top_category)
				// uni.setStorageSync('dish_top_category_obj', dish_top_category_obj)
			}catch(e){
				console.log('setStorageSync-dish_top_category:error');
			}
		},
		set_number_category(state, data) {
			let number_category = data;
			let number_category_obj = toObj(number_category,'id');
			state.$number_category = number_category;
			state.$number_category_obj = number_category_obj;
			try{
				uni.setStorageSync('number_category', number_category)
				// uni.setStorageSync('number_category_obj', number_category_obj)
			}catch(e){
				console.log('setStorageSync-number_category:error');
			}
		},
		set_daily_cost_category(state, data) {
			let daily_cost_category = data;
			let daily_cost_category_obj = toObj(daily_cost_category,'id');
			state.$daily_cost_category = daily_cost_category;
			state.$daily_cost_category_obj = daily_cost_category_obj;
			try{
				uni.setStorageSync('daily_cost_category', daily_cost_category)
				// uni.setStorageSync('daily_cost_category_obj', daily_cost_category_obj)
			}catch(e){
				console.log('setStorageSync-daily_cost_category:error');
			}
		},
		set_material_top_category(state, data) {
			state.$material_top_category = data;
			try{
				uni.setStorageSync('material_top_category', data)
			}catch(e){
				console.log('setStorageSync-material_top_category:error');
			}
		},
		set_material_second_category(state,data){
			state.$material_second_category = data;
			try{
				uni.setStorageSync('material_second_category', data)
			}catch(e){
				console.log('setStorageSync-material_second_category:error');
			}
		},
		set_material_purchase_unit_category(state, data) {
			state.$material_purchase_unit_category = data;
			let material_purchase_unit_category_obj = toObj(data,'id');
			state.$material_purchase_unit_category_obj = material_purchase_unit_category_obj;
			try{
				uni.setStorageSync('material_purchase_unit_category', data);
				// uni.setStorageSync('material_purchase_unit_category_obj', material_purchase_unit_category_obj);
			}catch(e){
				console.log('setStorageSync-material_purchase_unit_category:error');
			}
		},
		set_material_item_bom_unit_ratio(state, data) {
			state.$material_item_bom_unit_ratio = data;
			try{
				uni.setStorageSync('material_item_bom_unit_ratio', data)
			}catch(e){
				console.log('setStorageSync-material_item_bom_unit_ratio:error');
			}
		},
		set_msupplier(state, data){
			state.$msupplier = data;
			try{
				uni.setStorageSync('msupplier', data)
			}catch(e){
				console.log('setStorageSync-msupplier:error');
			}
			
		}
	},
	getters: {
		get_days(state) {
			return state.$days;
		},
		get_sales_cfg(state){
			return state.$sales_cfg;
		},
		get_material_item(state) {
			return state.$material_item;
		},
		get_material_obj(state) {
			let material_obj = state.$material_obj;
			return material_obj
		},
		get_number_category(state) {
			return state.$number_category
		},
		get_dinner_type_category(state) {
			return state.$dinner_type_category
		},
		get_cus_loc(state){
			return state.$cus_loc
		},
		get_cus_sales_cfg(state){
			return state.$cus_sales_cfg;
		},
		get_sales_category_list(state){
			return state.$sales_category_list;
		},
		get_dinner_type_category_obj(state) {
			return state.$dinner_type_category_obj;
		},
		get_dish_top_category(state) {
			return state.$dish_top_category
		},
		get_dish_top_category_obj(state) {
			return state.$dish_top_category_obj
		},
		get_number_category_obj(state) {
			return state.$number_category_obj
		},
		get_daily_cost_category(state) {
			return state.$daily_cost_category
		},
		get_daily_cost_category_obj(state) {
			return state.$daily_cost_category_obj
		},
		get_material_top_category(state) {
			return state.$material_top_category
		},
		get_material_second_category(state){
			return state.$material_second_category
		},
		get_material_purchase_unit_category(state) {
			return state.$material_purchase_unit_category
		},
		get_material_static_data(state) {
			let material_static_data = state.$material_static_data;
			return material_static_data
		},
		get_material_item_bom_unit_ratio(state) {
			let material_item_bom_unit_ratio = state.$material_item_bom_unit_ratio;
			return material_item_bom_unit_ratio
		},
		get_material_unit_obj(state){
			let material_item_bom_unit_ratio = state.$material_item_bom_unit_ratio;
			let unit_obj = {};
			material_item_bom_unit_ratio.forEach(v=>{
				let { material_id } = v;
				if(!unit_obj[material_id]){
					unit_obj[material_id] = [];
				}
				v.value = v.purchase_unit_id,
				v.label= state.$material_purchase_unit_category_obj[v.purchase_unit_id].name,
				unit_obj[material_id].push(v);
			})
			return unit_obj
		},
		get_stock(state){
			return state.$stock
		},
		get_stock_obj(state) {
			let stock_obj = state.$stock_obj;
			return stock_obj
		},
		get_plan_week_model_overall_show(state) {
			let plan_week_model_overall_show = state.$plan_week_model_overall_show;
			return plan_week_model_overall_show
		},
		get_dish_key_obj(state) {
			let dish_key_obj = state.$dish_key_obj;
			return dish_key_obj
		},
		get_msupplier(state){
			let msupplier = state.$msupplier;
			return msupplier
		},
		get_msupplier_obj(state){
			let msupplier = state.$msupplier;
			let msupplier_obj = toObj(msupplier,'id');
			return msupplier_obj
		},
		get_role(state) {
			let role = state.$role;
			return role
		},
		get_userInfo(state) {
			let userInfo = state.$userInfo;
			return userInfo
		},
		get_token(state) {
			let token = state.$token;
			return token
		},
		get_isLogin(state) {
			// let token = state.$token;
			// let userInfo = state.$userInfo;
			// return !!(token && userInfo.openid)
			let userInfo = state.$userInfo || {};
			return !!(userInfo.openid)
		},
		get_location(state) {
			let location = state.$location;
			return location
		},
	},
	actions: {
		modal(state, content) {
			let params = {
				title: '提示',
				content: '',
				confirmColor: '#675f91',
				showCancel: true
			};
			if (typeof content == 'string') {
				params.content = content;
			}
			if (typeof content == 'object') {
				Object.assign(params, content);
			}

			return new Promise((resolve, reject) => {
				uni.showModal({
					...params,
					success: function(res) {
						if (res.confirm) {
							resolve();
						} else if (res.cancel) {
							reject()
						}
					}
				});
			})
		},
		logout(state) {
			this.commit('set_userInfo', null);
			this.commit('set_token', null);
			this.commit('set_role', null);
		}
	}
})

export default store